<template>
    <div class="school">
        <div class="video-list">
            <div class="video">
                <video-dplayer
                    v-if="dataInfo"
                    ref="dplayer"
                    url="https://xzcs-public-files.oss-cn-beijing.aliyuncs.com/static/applet-new/bank/school_vedio.mp4"
                    pic="https://xzcs-public-files.oss-cn-beijing.aliyuncs.com/static/applet-new/bank/school_video.png"
                    :rate="rate"
                    isPlay="1"
                    @ratechange="ratechange"
                ></video-dplayer>
            </div>
            <div class="courses-list" :style="{paddingBottom: is_pay == '0' ? '67px' : '0'}">
                <div class="list-title">套餐课程</div>
                <div
                    v-for="(item, index) in dataList"
                    :key="index"
                    class="item"
                    @click="goVideo(item)"
                >
                    <img :src="item.cover_image" alt="" />
                    <div class="info">
                        <div>{{ item.name }}</div>
                        {{ item.section_num }}小节
                    </div>
                </div>
                <div v-if="dataInfo && is_pay == '0'" class="pay">
                    <div class="moeny">
                        价格：<span
                            ><span style="font-size: 16px">¥</span
                            >{{ dataInfo.money }}</span
                        >
                    </div>
                    <div class="pay-button" @click="goPay">购买学习</div>
                </div>
            </div>
        </div>
        <div v-if="dataInfo" class="introduce">
            <div class="title">{{ dataInfo.name }}</div>
            <div class="content" v-html="dataInfo.brief"></div>
        </div>

        <el-dialog :visible.sync="payShow" width="396px">
            <div slot="title" class="dialog-title">课程购买</div>
            <div v-if="dataInfo" class="pay-content">
                <div class="pay-info">
                    <div class="name">{{ dataInfo.name }}</div>
                    <div class="right">
                        <div>
                            价格：<span>￥{{ dataInfo.money }}</span>
                        </div>
                        课程有效期：1年
                    </div>
                </div>
                <div
                    v-loading="qrLoading"
                    :element-loading-text="loadingText"
                    class="code"
                >
                    <vue-qr
                        ref="qrCode"
                        :text="qrText"
                        :logoSrc="logoSrc"
                        :color-dark="randomColor"
                        :margin="0"
                        width="160"
                        height="160"
                    ></vue-qr>
                </div>
                <div class="pay-code-type">
                    <img
                        src="~/assets/images/userCenter/pay-wx.png"
                        class="icon"
                        alt=""
                    />
                    <img
                        src="~/assets/images/userCenter/pay-zfb.png"
                        class="icon"
                        alt=""
                    />
                    <span>微信/支付宝支付</span>
                </div>
                <div class="pay-code-agreement">
                    支付即同意&nbsp;《<span @click="goPayXieyi">支付协议</span
                    >》<template
                        >《<span @click="goxieyi(2)">会员协议</span>》</template
                    >
                </div>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { mapState, mapMutations } from "vuex";
import { getanalysedata2 } from "@/assets/api/indexApi";

import {
    get_paycourselist,
    pc_buy_collegecourse,
    collegecourse_paydetail,
} from "@/assets/api/schoolApi.js";

import videoDplayer from "@/components/school/videoDplayer.vue";

function getQueryParam(name, url) {
    name = name.replace(/[\[\]]/g, "\\$&"); // 替换方括号以正确处理正则表达式的特殊字符
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return "";
    return decodeURIComponent(results[2].replace(/\+/g, " "));
}

export default {
    components: {
        videoDplayer,
    },
    metaInfo() {
        return {
            title: this.mateInfo.title,
            meta: [
                { name: "keywords", content: "小竹,财税,小竹学院,财税热点" },
            ],
        };
    },
    layout: "home",
    data() {
        return {
            payShow: false,

            dataInfo: null,
            dataList: [],
            is_pay: "",

            qrText: "",
            qrLoading: true,
            loadingText: "",
            logoSrc: "",
            randomColor: "black",

            timer: null,

            // 初始倍速
            rate: 1,
        };
    },
    computed: {
        ...mapState("common", {
            userInfo: (state) => state.userInfo,
        }),
    },
    watch: {},
    mounted() {
        if (this.$route.params.id) {
            this.getData();
        }
    },
    beforeMount() {
        this.$options.components["vue-qr"] = () => import("vue-qr");
    },
    methods: {
        async getData() {
            const res = await get_paycourselist({
                id: this.$route.params.id,
            });
            if (res.code == 1) {
                this.dataInfo = res.data.coourse[0];
                this.dataList = res.data.course_lists;
                this.is_pay = res.data.is_pay;
            }
        },

        async goPay() {
            this.qrLoading = true;
            this.payShow = true;
            this.loadingText = "拼命生成二维码中";
            const res = await pc_buy_collegecourse({
                combo_id: this.dataInfo.id,
            });
            if (res.code == 1) {
                let that = this;
                this.qrText = res.data;
                this.qrLoading = false;
                if (this.timer) {
                    clearInterval(this.timer);
                    this.timer = null;
                }

                this.timer = setInterval(() => {
                    that.getPaydetail(getQueryParam("order_sn", res.data));
                }, 3000);
            }
        },

        async getPaydetail(order_sn) {
            if (!this.payShow) {
                clearInterval(this.timer);
                this.timer = null;
            }
            const res = await collegecourse_paydetail({
                order_no: order_sn,
            });
            if (res && res.code == 1 && res.data && res.data.pay_time) {
                clearInterval(this.timer);
                this.timer = null;
                this.payShow = false;
                this.getData();
                this.$confirm("购买成功", "提示", {
                    confirmButtonText: "知道了",
                    closeOnClickModal: false,
                    closeOnPressEscape: false,
                    closeOnHashChange: false,
                    showCancelButton: false,
                });
            }
        },

        goVideo(item) {
            if(this.is_pay == '0'){
                this.$confirm("请先购买课程", "提示", {
                    confirmButtonText: "知道了",
                    closeOnClickModal: false,
                    closeOnPressEscape: false,
                    closeOnHashChange: false,
                    showCancelButton: false,
                });
                return
            }
            this.$router.push({
                path: "/school/hot/" + item.id + "?type=1",
            });
        },

        goPayXieyi() {
            window.open(
                "https://api.xiaozhucaishui.com/xieyi/pay.html",
                "_blank"
            );
        },
        goxieyi(index) {
            this.$router.push({
                path: "/useragreement?index=" + index,
            });
        },

        // 倍速切换
        ratechange(rate) {
            this.rate = rate;
        },
    },
};
</script>

<style lang="scss" scoped>
.school {
    width: 100%;
    min-height: 100vh;
    padding-top: 75px;
    background: #0f0f1e;
}
.video-list {
    width: 1215px;
    margin: 0 auto;
    background: #000000;
    display: flex;
    position: relative;

    .video {
        width: 903px;
        height: 520px;
        background: #000000;
        border-radius: 10px 0px 0px 10px;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    .courses-list {
        width: 312px;
        height: 520px;
        background: #202127;
        border-radius: 0px 10px 10px 0px;
        padding: 10px 10px 0;
        overflow-y: auto;
        &::-webkit-scrollbar {
            width: 3px;
        }

        &::-webkit-scrollbar-thumb {
            border-radius: 10px;
            background: #dcdfe6;
        }

        &::-webkit-scrollbar-track {
            border-radius: 0;
        }
        .list-title {
            font-weight: 700;
            font-size: 18px;
            color: #ffffff;
            margin-bottom: 10px;
        }
        .item {
            width: 100%;
            height: 86px;
            background: #ffffff;
            border-radius: 5px;
            margin-bottom: 10px;
            padding: 10px;
            display: flex;
            cursor: pointer;
            img {
                min-width: 104px;
                width: 104px;
                height: 100%;
                background: #666666;
                object-fit: cover;
                border-radius: 10px;
            }
            .info {
                font-weight: 400;
                padding-left: 10px;
                font-size: 12px;
                color: #9e9e9e;
                div {
                    height: 50px;
                    font-weight: 400;
                    font-size: 16px;
                    color: #333333;
                    line-height: 21px;
                }
            }
        }
    }
    .pay {
        position: absolute;
        bottom: 0;
        right: 0;
        width: 312px;
        height: 67px;
        background: #ffffff;
        box-shadow: 0px -2px 10px 0px rgba(0, 0, 0, 0.3);
        border-radius: 0px 0px 10px 0px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding-left: 20px;
        padding-right: 10px;
        .moeny {
            font-weight: 600;
            font-size: 21px;
            color: #06061b;
            span {
                font-size: 24px;
                color: #fa5151;
            }
        }
        .pay-button {
            width: 110px;
            height: 33px;
            line-height: 33px;
            text-align: center;
            cursor: pointer;
            background: linear-gradient(45deg, #1c87f3 0%, #599dff 100%);
            border-radius: 5px;
            font-weight: 400;
            font-size: 18px;
            color: #ffffff;
            user-select: none;
            &:hover {
                opacity: 0.8;
            }
            &:active {
                opacity: 0.9;
            }
        }
    }
}

.introduce {
    width: 1215px;
    margin: 15px auto 0;
    background: #ffffff;
    border-radius: 10px;
    min-height: 125px;
    padding: 14px;
    .title {
        font-weight: 600;
        font-size: 18px;
        color: #06061b;
    }

    .content {
        font-weight: 400;
        font-size: 14px;
        color: #06061b;
        line-height: 22px;
        padding-top: 15px;
    }
}

.dialog-title {
    font-family: Alimama ShuHeiTi, Alimama ShuHeiTi;
    font-weight: 700;
    font-size: 20px;
    color: #693f16;
    text-align: center;
}

.pay-content {
    .pay-info {
        display: flex;
        align-items: center;
        justify-content: space-between;
        .name {
            font-weight: 400;
            font-size: 18px;
            color: #333333;
        }
        .right {
            font-weight: 400;
            font-size: 14px;
            color: #333333;
            div {
                font-weight: 500;
                font-size: 18px;
                color: #333333;
                padding-bottom: 6px;
                span {
                    font-size: 20px;
                    color: #ff5448;
                }
            }
        }
    }
    .code {
        display: block;
        margin: 16px auto 27px;
        width: 160px;
        height: 160px;
        div {
            width: 160px;
            height: 160px;
        }
    }
    .pay-code-type {
        display: flex;
        align-items: center;
        justify-content: center;
        font-weight: 500;
        font-size: 16px;
        color: #666666;
        img {
            width: 20px;
            height: 20px;
            margin-right: 3px;
        }
    }
    .pay-code-agreement {
        font-weight: 400;
        font-size: 12px;
        color: #9e9e9e;
        text-align: center;
        margin-top: 20px;
        span {
            cursor: pointer;
            &:hover {
                color: #1c87f3;
                text-decoration: underline;
            }
        }
    }
}
</style>